<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllerextraction //extends AppController
{

	function __construct(  )
	{
		
		$task	=	( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			case 'getItens':
				
				
				try {
        						
					$e =	json_decode($_GET['parameters']);
				
					$result =	$this->getItens($e[0]->job->extraction);
					
					//echo $e[0]->job->extraction->hostname;
					$_GET['notation']=$e[0]->job->extraction->notation;
					//var_dump( $result );
					
					application::executeView( $task, $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			    catch (Exception $e) {
			        echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    }
				
		
				
				
				break;
			case 'create':
				
				application::executeView( 'edit' );
				
				break;
			case 'save':
				
				echo $this->save( $_POST );
				application::executeView( 'webservice' );
				
				break;
			case 'authentication':
				
				echo $this->authentication( $_POST['email'], $_POST['password'] );
				application::executeView( 'webservice' );
				
				break;
			case 'logout':
				
				echo $this->logout( );
				application::executeView( 'restrict' );
				
				break;
			case 'restrict':
				
				application::executeView( $task );
				
				break;	
			default:
				
				echo "view não definida.";
				break;
		}
		
	}
	
	public function getItens( $args ){
				
		$result=false;
				
		//$model	=	application::getModel('user');
		
		$db = application::getDb( $args->type);
		
		$db->setParameters($args->hostname, $args->port, 
								$args->username, $args->password,
								$args->databasename);
		
		$result = $db->getQuery($args->query);

		return $result;
		
	}
	
	
	public function getSchemaTable( $query ){
				
		$result=false;
				
		//$model	=	application::getModel('user');
		
		$db = application::getDb( $args->type);
		
		$db->setParameters($args->hostname, $args->port, 
								$args->username, $args->password,
								$args->databasename);
		
		$query=	"select " 
                	."ic.column_name, "
                        ."CASE WHEN ic.character_maximum_length IS NULL THEN 0 ELSE ic.character_maximum_length END,"
                        ."pd.description,"
                        ."ic.udt_name"
                       . " "
                        ."from INFORMATION_SCHEMA.COLUMNS ic"
                        .""
                        ." inner join pg_class pc ON pc.relname=ic.table_name"
                       . ""
                        ." left join pg_description pd ON pd.objoid = pc.oid"
                       . ""
                       . " where"
                        .""
                       . " ic.table_name = '"+$query_table+"'"
                       . " "
                        ." order by ic.ordinal_position asc";
		
		//$result = $db->getQuery($query);

		return $result;
		
	}
	
	
	public function getTableFromInQuery( $query ){
				
		$result=false;
				
		$query=	substr($query, 0, strpos("FROM", $query));

		return $result;
		
	}

	
	public function authentication( $email, $password ){
				
		$model	=	application::getModel('user');
		
		$result	=	$model->authentication( $email, $password );
		
		if( $result )
			$result=1;//application::setAttribute('save', '1');
		else
			$result=2;//application::setAttribute('save', '2');
		
		return $result;	
		
	}
	
	public function logout( ){
				
		application::session_logout();
		
	}
	
}